/**
 * @Title:  AuthMechanismActivity.java 
 * @Copyright:  XXX Co., Ltd. Copyright YYYY-YYYY,  All rights reserved 
 * @Description:  TODO<请描述此文件是做什么的> 
 * @author:  MeiYi 
 * @data:  2016-1-5 上午11:24:06 
 * @version:  V1.0 
 */

package artid.com.arttogo.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

import java.util.ArrayList;

import artid.com.arttogo.R;
import artid.com.arttogo.activity.WebViewActivity;
import artid.com.arttogo.adapter.ArtNewsAdapter;
import artid.com.arttogo.adapter.ListAdapter;
import artid.com.arttogo.bean.NewsList;
import artid.com.arttogo.bean.ShareItem;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.AppUrl;
import artid.com.arttogo.network.BitmapCache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.view.pulllist.PullToRefreshLayout;
import artid.com.arttogo.view.pulllist.PullableListView;

/**
 * TODO<请描述这个类是干什么的>
 * 艺术咨询
 * @author 熊彬
 * @data: 2016-1-5 上午11:24:06
 * @version: V1.0
 */
public class CollecterNewsActivity extends BaseFragment {
	private Context mContext;
	private PullableListView mListView;
	private ArtNewsAdapter adapter;
	private ImageView back;
	public ArrayList<NewsList> list;
	private TextView title;
	private RelativeLayout type;
	private ListView listview;
	private ListAdapter typeAdapter;
	PullToRefreshLayout mPullLayout;
	int pageIndex = 1;
	String strType = "0";
	private ImageLoader mImageLoader;
	int  pagesize = 20;
	String id = "";
	View mView;
	/*
	 * (non-Javadoc)
	 * @see com.sigmatrix.artworkart.BaseActivity#onCreate(android.os.Bundle)
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.activity_collecter_newsletter, null);
		mContext = getActivity();
		mListView = (PullableListView) mView.findViewById(R.id.listivew);
		RequestQueue mQueue = Volley.newRequestQueue(mContext);
		mImageLoader = new ImageLoader(mQueue, new BitmapCache());
		hint = (LinearLayout) mView.findViewById(R.id.ll_hint);
		hint.setVisibility(View.GONE);
		mPullLayout = (PullToRefreshLayout)mView.findViewById(R.id.refresh_view);
		mPullLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

				@Override
				public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
					// TODO Auto-generated method stub
					pageIndex = 1;
					requestGradeList();
				}

				@Override
				public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
					// TODO Auto-generated method stub
					// pullToRefreshLayout.setBottomDismiss();
					pageIndex++;
					requestGradeList();
				}
			});

		requestGradeList();
		return mView;
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
			Bundle args = getArguments();
			id = args != null ? args.getString("id") : "";
			super.onCreate(savedInstanceState);

	}

	private void requestGradeList() {
		VolleyBasic.getInstance((Activity)mContext)
		.post(Cache.newsList, VolleyMap.getInstance().newsList2(String.valueOf(pageIndex),id),
				NewsList.class,
				new VolleyCallback() {
					
					@Override
					public void onSuccess(VolleyData vd) {
						// TODO Auto-generated method stub
						mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
						list = (ArrayList<NewsList>) vd.getList();
						if (list.isEmpty()){
							if (pageIndex==1) {
								mListView.setVisibility(View.INVISIBLE);
								hint.setVisibility(View.VISIBLE);
							}
							return;
						}
						hint.setVisibility(View.GONE);
						if (mListView.getVisibility()== View.INVISIBLE) {
							mListView.setVisibility(View.VISIBLE);
						}
						if (pageIndex!=1) {
							adapter.addList(list);
							setItemClick();
							return;
						}
						adapter = new ArtNewsAdapter(mContext,list);
						mListView.setAdapter(adapter);
						setItemClick();
					}
					
					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub
						mPullLayout.loadmoreFinish(mPullLayout.FAIL);
					}
				});
	}
	
	void setItemClick(){
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
									long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mContext, WebViewActivity.class);
				ShareItem si = new ShareItem();
				NewsList item = adapter.listData.get(arg2);
				si.setId(item.getId());
				if(item.getImgUrl()!=null && item.getImgUrl().size()!=0) {
					si.setImageUrl(item.getImgUrl().get(0));
				}else {
					si.setImageUrl("");
				}
				si.setContent(item.getTitle());
				si.setTitle(item.getTitle());
				si.setUrl(Cache.base+"/h5/forensic-news.html?id="+item.getId());
				intent.putExtra("share", si);
				intent.putExtra("id", item.getId());
				intent.putExtra("type", AppUrl.AUTH_NEWS);
				startActivity(intent);
			}
			
		});
	}
	
	
}
